package cc.rengu.igas.bomp.common.dao.impl;

import cc.rengu.igas.bomp.common.dao.MchntFeeInfoMapper;
import cc.rengu.igas.bomp.common.entity.MchntFeeInfo;
import cc.rengu.oltp.service.common.constant.AppConfigConstant;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.utility.util.DbsUtil;
import cc.rengu.oltp.utility.util.XmlConfigUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.cache.UnifiedCache;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by 王成 on 2020/3/2
 */
public class MchntFeeInfoMapperImpl implements MchntFeeInfoMapper {
    private RgLogger rglog = RgLog.getLogger(this.getClass().getName());
    private String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppConfigConstant.DATABASE_CONF_NODE, AppConfigConstant.MCMP_POOL_NAME);

    @Override
    public int insertMchntFeeInfo(MchntFeeInfo mchntFeeInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "insertMchntFeeInfo");
        int iReturnCode = dbsUtil.dbsExecuteTransactionSql(sqlStr, mchntFeeInfo, null);
        if (0 != iReturnCode) {
            rglog.error("登记商户费率信息失败,instId:<{}>,mchntNo:<{}>,iReturnCode:<{}>", mchntFeeInfo.getInstId(), mchntFeeInfo.getMchntNo(), iReturnCode);
        }
        return iReturnCode;
    }

    @Override
    public int updateMchntFeeInfoById(MchntFeeInfo mchntFeeInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "updateMchntFeeInfoById");
        int iReturnCode = dbsUtil.dbsExecuteTransactionSql(sqlStr, mchntFeeInfo, null);
        if (0 != iReturnCode) {
            rglog.error("登记商户费率信息失败,instId:<{}>,mchntNo:<{}>,id:<{}>,iReturnCode:<{}>", mchntFeeInfo.getInstId(), mchntFeeInfo.getMchntNo(), mchntFeeInfo.getId(), iReturnCode);
        }
        return iReturnCode;
    }

    @Override
    public MchntFeeInfo selectMchntFeeInfoById(String instId, String id) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectMchntFeeInfoById");
        MchntFeeInfo mchntFeeInfo = new MchntFeeInfo();
        mchntFeeInfo.setInstId(instId);
        mchntFeeInfo.setId(id);
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, mchntFeeInfo, mchntFeeInfo);
        if (0 != iReturnCode) {
            rglog.error("获取商户费率信息失败,instId:<{}>,id:<{}>,iReturnCode:<{}>", instId, id, iReturnCode);
            return null;
        }
        return mchntFeeInfo;
    }

    @Override
    public List<MchntFeeInfo> selectMchntFeeInfoByFeeType(String instId, String mchntNo, String baseProdCode, String feeType) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectMchntFeeInfoByFeeType");
        MchntFeeInfo mchntFeeInfo = new MchntFeeInfo();
        mchntFeeInfo.setInstId(instId);
        mchntFeeInfo.setMchntNo(mchntNo);
        mchntFeeInfo.setBaseProdCode(baseProdCode);
        mchntFeeInfo.setFeeType(feeType);
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, mchntFeeInfo, MchntFeeInfo.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("获取商户费率信息失败,instId:<{}>,mchntNo:<{}>,baseProdCode:<{}>,feeType:<{}>", instId, mchntNo, baseProdCode, feeType);
            return null;
        }
        return objectList.stream().map(item -> (MchntFeeInfo) item).collect(Collectors.toList());
    }

    @Override
    public List<MchntFeeInfo> selectMchntFeeInfoByMchntNo(String instId, String mchntNo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectMchntFeeInfoByMchntNo");
        MchntFeeInfo mchntFeeInfo = new MchntFeeInfo();
        mchntFeeInfo.setInstId(instId);
        mchntFeeInfo.setMchntNo(mchntNo);
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, mchntFeeInfo, MchntFeeInfo.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("获取商户费率信息失败,instId:<{}>,mchntNo:<{}>", instId, mchntNo);
            return null;
        }
        return objectList.stream().map(item -> (MchntFeeInfo) item).collect(Collectors.toList());
    }


    @Override
    public List<MchntFeeInfo> selectMchntFeeInfoByBaseProdCode(String instId, String mchntNo, String baseProdCode) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectBompMchntFeeInfoListByBaseProdCode");
        MchntFeeInfo mchntFeeInfo = new MchntFeeInfo();
        mchntFeeInfo.setInstId(instId);
        mchntFeeInfo.setMchntNo(mchntNo);
//        mchntFeeInfo.setProdCode(prodCode);
        mchntFeeInfo.setBaseProdCode(baseProdCode);
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, mchntFeeInfo, MchntFeeInfo.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("获取商户费率信息失败,instId:<{}>,mchntNo:<{}>,baseProdCode:<{}>", instId, mchntNo, baseProdCode);
            return null;
        }
        return objectList.stream().map(item -> (MchntFeeInfo) item).collect(Collectors.toList());
    }


    @Override
    public MchntFeeInfo selectMchntFeeInfoByCondition(String instId, String mchntNo, String baseProdCode, String feeType, String date) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectBompMchntFeeInfoByCondition");
        MchntFeeInfo mchntFeeInfo = new MchntFeeInfo();
        mchntFeeInfo.setInstId(instId);
        mchntFeeInfo.setMchntNo(mchntNo);
        mchntFeeInfo.setBaseProdCode(baseProdCode);
        mchntFeeInfo.setFeeType(feeType);
        mchntFeeInfo.setFeeEffectiveDate(date);
        mchntFeeInfo.setFeeExpiryDate(date);
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, mchntFeeInfo, mchntFeeInfo);
        if (0 != iReturnCode) {
            rglog.error("获取商户费率信息失败,instId:<{}>,mchntNo:<{}>,baseProdCode:<{}>,feeType:<{}>,date:<{}>,iReturnCode:<{}>", instId, mchntNo, baseProdCode, feeType, date, iReturnCode);
            return null;
        }
        return mchntFeeInfo;
    }
}
